import {request} from '@inbiz/utils';

type Iparams = {
  appId: string;
  key: string;
  pageInfo: {
    index: number;
    size: number;
  };
  conditions?: {
    index: number;
    connector: 0 | 1;
    condition: {
      connector: 0 | 1;
      field: string;
      modelName?: string;
      operator:
        | 'eq'
        | 'neq'
        | 'gt'
        | 'get'
        | 'lt'
        | 'lte'
        | 'like'
        | 'nlike'
        | 'nstartwith'
        | 'startwith'
        | 'endwith'
        | 'nendwith'; //| 'in' | 'nin',
      value?: unknown;
    }[];
  }[];
};

/**
 * 查询字段列表
 * @param params
 */
export async function getFieldList(params: { appId: string; key: string }) {
  return request(
    `/inbiz/api/services/modelengine/v${InbizConfig.API_VERSION}/modelQueryAdapter/controlField?appId=${params.appId}&key=${params.key}`,
  );
}

/**
 * 查询表格数据
 * @param params
 */
export async function getTableList(params: Iparams) {
  return request(
    `/inbiz/api/services/front/modelengine/v${InbizConfig.API_VERSION}/modelQueryAdapter/queryDatas`,
    {
      method: 'POST',
      data: {
        ...params,
        DropDownList: true,
      },
    },
  );
}

/**
 * 根据值查询数据详情
 * @param appId appId
 * @param ids 搜索id,多个用','隔开
 * @param queryKey 查询器id,多个用','隔开
 */
export async function getTableInfo(param: { appId: string; ids: string; queryKey: string }) {
  return request(
    `/inbiz/api/services/modelengine/v${InbizConfig.API_VERSION}/control/dropDownListData/${param.appId}`,
    {
      params: { ...param },
    },
  );
}

/**
 * 根据值查询数据详情(接口工厂)
 * @param appId appId
 * @param id 搜索id,多个用','隔开
 * @param key 查询器id,多个用','隔开
 */
export async function getTableInfoApi(param: { appId: string; id: string; key: string }) {
  return request(
    `/inbiz/api/services/front/modelengine/v${InbizConfig.API_VERSION}/businessData/dataInterface`,
    {
      method: 'POST',
      data: param,
    },
  );
}

/**
 * 根据值查询模型
 * @param appId appId
 */
export async function getModelList(appId: string) {
  return request(
    `/inbiz/api/services/modelengine/v${InbizConfig.API_VERSION}/model/publishs/${appId}?pageIndex=1&pageSize=100`,
  );
}

/**
 * 根据值查询模型详情
 * @param appId appId
 * @param modelKeys model key
 */
export async function getModelInfo(appId: string, modelKeys: string) {
  var modelKeysArray = modelKeys.split('&modelKeys=');
  return request(`/inbiz/api/services/modelengine/v${InbizConfig.API_VERSION}/model/manyFields`, {
    method: 'post',
    data: {
      appId,
      modelKeys: modelKeysArray,
      includeSystem: true,
    },
  });
}
